package com.fili.gui.sources;

import com.fili.core.sources.MappedSource;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 17, 2006
 * Time: 3:31:01 PM
 * Revision: 0
 */
public class SourcePane extends JTextPane
{
    private StyledDocument doc;
    private Style sty_normal;
    private Style sty_keyword;
    private Style sty_disabled;


    public SourcePane()
    {
        doc = getStyledDocument();

        sty_normal = doc.getStyle(StyleContext.DEFAULT_STYLE);
        StyleConstants.setFontFamily(sty_normal, "Monospaced");
        StyleConstants.setFontSize(sty_normal, 12);

        sty_keyword = doc.addStyle("keyword", sty_normal);
        StyleConstants.setBold(sty_keyword, true);
        StyleConstants.setForeground(sty_keyword, Color.blue);

        sty_disabled = doc.addStyle("disabled", sty_normal);
        StyleConstants.setItalic(sty_disabled, true);
        StyleConstants.setForeground(sty_disabled, Color.red);
    }

    public void setSource(MappedSource src)
    {
        
        try
        {
            doc.remove(0,doc.getLength());
            for (int i = 0; i < src.getSize(); i++)
            {
                if (src.getMapping(i) < 0)
                    doc.insertString(doc.getLength(), src.getLine(i) + "\n", sty_disabled);
                else
                    doc.insertString(doc.getLength(), src.getLine(i) + "\n", sty_normal);
            }
        }
        catch (BadLocationException e)
        {
            e.printStackTrace();
        }
    }
}
